
package com.probiz.estoresa.content.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.catalog.service.ContentCategoryManager;
import com.probiz.estore.common.model.catalog.ContentCategory;
import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.content.model.ContentSearchCriteria;
import com.probiz.estore.content.service.ContentManager;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;

public class ContentSelectorController extends
		GenericStoreAdminController<Content> {

	private ContentManager			contentManager			= null;
	private ContentCategoryManager	contentCategoryManager	= null;

	public void setContentManager(ContentManager contentManager) {
		this.contentManager = contentManager;
	}

	public void setContentCategoryManager(
			ContentCategoryManager contentCategoryManager) {
		this.contentCategoryManager = contentCategoryManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(Content entity) {
		return entity.getContentTitle();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = contentManager;
	}

	@Override
	protected void preSave(HttpServletRequest request, Content entity,
			BindException errors) {

	}

	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("content/contentSelector");
		ContentCategory treeRoot = contentCategoryManager.getById(1);
		Set<ContentCategory> categorySet = treeRoot.getChildren();
		List<ContentCategory> categoryList = new ArrayList<ContentCategory>();
		categoryList.addAll(categorySet);
		mv.addObject("categoryList", categoryList);
		mv.addObject("treeRoot", treeRoot);
		return mv;
	}

	public ModelAndView search(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("content/contentSelectorResults");
		SearchCriteria searchCriteria = createSearchCriteria(request);
		ContentSearchCriteria contentSearchCriteria = new ContentSearchCriteria();
		try {
			// 绑定参数
			bind(request, contentSearchCriteria);
			List<Content> contentList = contentManager.searchByCriteria(
					contentSearchCriteria, searchCriteria);
			mv.addObject("contentList", contentList);
			mv.addObject("contentSearchCriteria", contentSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;

	}

}
